var myChart1 = null;
var myChart2 = null;
var myChart3 = null;
var myChart4 = null;
var myChart5 = null;

$(function(){
	$("#myTab").Huitab({});
	initDivSize();
	myChart1 = echarts.init(document.getElementById("oneDaySign"));
	myChart2 = echarts.init(document.getElementById("sevenDaySign"));
	myChart3 = echarts.init(document.getElementById("fifteenDaySign"));
	myChart4 = echarts.init(document.getElementById("thirtyDaySign"));
	myChart5 = echarts.init(document.getElementById("customDaySign"));
	myChart1.setOption(getOption(1));
	myChart2.setOption(getOption(2));
	myChart3.setOption(getOption(3));
	myChart4.setOption(getOption(4));
	myChart5.setOption(getOption(5));
	// 第一次进来默认加载第一个tab页数据
	querySignData(1);
});

/**
 * 异步查询数据，并初始化图表
 * @param type 1:24小时 2:7天 3:15天 4:30天 5:自定义
 * @param refreshFlag 刷新标识，true表示刷新
 */
function querySignData(type, refreshFlag) {
	var myChart = getChartObject(type);
	var params = {type: type};
	if(type == 5) { // 自定义模式
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		if(startDate == "") {
			parent.layer.alert("请选择起始日期！");
			return;
		}
		if(endDate == "") {
			parent.layer.alert("请选择结束日期！");
			return;
		}
		// 查询时禁用按钮，查询完成后，再启用，避免重复点击
		$("#customBtn").removeClass("btn-primary").addClass("disabled").attr("disabled", true);
		$("#startDate").attr("disabled", true);
		$("#endDate").attr("disabled", true);
		params.startDate = startDate;
		params.endDate = endDate;
	} else {
		// 已有数据则不需要重复加载
		if(!refreshFlag && myChart.getOption().series[0].data.length > 0) {
			return;
		}
	}
	myChart.showLoading();
	$.post(path + "/signController/querySignInfo", params, function(returnValue) {
		$("#customBtn").removeClass("disabled").addClass("btn-primary").attr("disabled", false);
		$("#startDate").attr("disabled", false);
		$("#endDate").attr("disabled", false);
		myChart.hideLoading();
		if(returnValue.code == "1") {
			myChart.setOption({
				xAxis: {data: returnValue.categories},
				series: [
			    	{name: "PC端", data: returnValue.data.PC_DATA},
			        {name: "移动端", data: returnValue.data.MOBILE_DATA},
			        {name: "所有总数", data: returnValue.data.ALL_DATA}
			    ]
			});
		} else {
			parent.layer.alert(returnValue.errorMsg);
		}
	}, "json");
}

/**
 * 导出会员签到详情
 */
function exportSignDetail(type) {
	var url = path + "/signController/exportSignDetail?type=" + type;
	if(type == 5) {
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		if(startDate == "") {
			parent.layer.alert("请选择起始日期！");
			return;
		}
		if(endDate == "") {
			parent.layer.alert("请选择结束日期！");
			return;
		}
		url += "&startDate=" + startDate;
		url += "&endDate=" + endDate;
	}
	$("#exportFrame").attr("src", url);
}

/**
 * 初始化图标配置项
 * @param type
 * @returns {___anonymous2641_3693}
 */
function getOption(type) {
	// 横坐标数据
	var xAxis_data = [];
	switch(type) {
		case 1:
			xAxis_data = get24Hour();
			break;
		case 2:
			xAxis_data = getNearDate(7);
			break;
		case 3:
			xAxis_data = getNearDate(15);
			break;
		case 4:
			xAxis_data = getNearDate(30);
			break;
	}
	
	var option = {
		tooltip: {trigger: "axis"},
	    legend: {data: ["PC端", "移动端", "所有总数"]},
	    toolbox: {
	    	itemSize: 20,
	    	right: 100,
	    	feature:{
	    		myTool_refresh: {
	    			icon: "image://" + path + "/resources/images/refresh.png",
	    			title: "刷新",
	    			onclick: function () {
	    				querySignData(type, true);
	                }
	    		},
	    		myTool_export: {
	    			icon: "image://" + path + "/resources/images/export.png",
	    			title: "导出",
	    			onclick: function () {
	    				exportSignDetail(type);
	                }
	    		},
	    		dataView: {readOnly: true}
	    	}
	    },
	    xAxis: {type: "category", boundaryGap: false, data: xAxis_data},
	    yAxis: {type: "value"},
	    dataZoom: [
	        {type: "slider", xAxisIndex: 0},
	        {type: "inside", xAxisIndex: 0}
	    ],
	    series: [
	    	{type: "line", smooth: true, data: [], name: "PC端"},
	        {type: "line", smooth: true, data: [], name: "移动端"},
	        {type: "line", smooth: true, data: [], name: "所有总数", label: {normal: {show: true}}}
	    ]
	};
	return option;
}

/**
 * 根据查询类型获取图标对象
 * @param type
 * @returns
 */
function getChartObject(type) {
	switch(type) {
		case 1:
			return myChart1;
		case 2:
			return myChart2;
		case 3: 
			return myChart3;
		case 4: 
			return myChart4;
		case 5: 
			return myChart5;
	}
}

/**
 * 初始化图表控件的宽高
 */
function initDivSize() {
	var width = document.body.clientWidth - 20;
	var height = document.body.clientHeight - 94;
	$("#oneDaySign").width(width);
	$("#oneDaySign").height(height);
	$("#sevenDaySign").width(width);
	$("#sevenDaySign").height(height);
	$("#fifteenDaySign").width(width);
	$("#fifteenDaySign").height(height);
	$("#thirtyDaySign").width(width);
	$("#thirtyDaySign").height(height);
	$("#customDaySign").width(width);
	$("#customDaySign").height(height - 30);
}

/***
 * 窗口大小改变时，重新绘制图表
 */
window.onresize = function() {
	initDivSize();
	if(myChart1) {
		myChart1.resize();
	}
	if(myChart2) {
		myChart2.resize();
	}
	if(myChart3) {
		myChart3.resize();
	}
	if(myChart4) {
		myChart4.resize();
	}
	if(myChart5) {
		myChart5.resize();
	}
}

/**
 * 获取两个日期之间的日期
 * @param begin 开始日期 格式：yyyy-MM-dd
 * @param end 结束日期 格式：yyyy-MM-dd
 * @returns {Array}
 */
function getDateBetweenTwoDate(begin, end) {
	var dates = [];
	begin = begin.split("-");
	end = end.split("-");
	begin = new Date(begin[0], Number(begin[1]) - 1, begin[2]);
	end = new Date(end[0], Number(end[1]) - 1, end[2]);
	var endTime = end.getTime();
	while(endTime - begin.getTime() >= 0) {
		dates.push(begin.format("yyyy-MM-dd"));
		begin.setDate(begin.getDate() + 1);
	}
	return dates;
}

/**
 * 获取最近几天的日期
 * @param days 天数
 * @returns {Array}
 */
function getNearDate(days) {
	var datesArr = [];
	var myDate = new Date();
	for(var i = 0; i < days; i ++) {
		datesArr.unshift(myDate.format("yyyy-MM-dd"));
		myDate.setDate(myDate.getDate() - 1);
	}
	return datesArr;
}

/**
 * 获取24小时
 * @returns {Array}
 */
function get24Hour() {
	var hoursInDay = [];
	var myDate = new Date();
	var h = myDate.getHours();
	for(var i = 0; i < 25; i ++) {
		hoursInDay.push((h < 10 ? "0" + h : h) + ":00:00");
		h = h == 23 ? 0 : ++ h;
	}
	return hoursInDay;
}

/**
 * 格式化日期
 */
Date.prototype.format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1,
		"d+": this.getDate(),
		"h+": this.getHours(),
		"m+": this.getMinutes(),
		"s+": this.getSeconds(),
		"q+": Math.floor((this.getMonth()+3)/3),
		"S": this.getMilliseconds()
	};
	if(/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	}
	for(var k in o) {
		if(new RegExp("("+ k +")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
		}
	}
	return fmt; 
}